2016-05-06 5 views
0

spring mvc、xmlベースの設定でコントローラの特定のメソッドにリダイレクトする方法は?spring mvc、xmlベースの設定でコントローラの特定のメソッドにリダイレクトする方法は?

私はJSPからデータを送信しており、コントローラ内の特定のメソッドに行きたいと思っています。 私はMAVENなしでXMLベースの設定を使用しています。

ありがとうございます。

答えて

0

PersonからPersonを表示するJSPファイルを作成します。 ファイル:/WebContent/WEB-INF/jsp/person.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<html> 
<body> 
<form:form method="post" action="addPerson.html"> 

    <table> 
    <tr> 
     <td><form:label path="firstname">First Name</form:label></td> 
     <td><form:input path="firstname" /></td> 
    </tr> 
    <tr> 
     <td><form:label path="lastname">Last Name</form:label></td> 
     <td><form:input path="lastname" /></td> 
    </tr> 
    <tr> 
     <td colspan="2"> 
      <input type="submit" value="Add Person"/> 
     </td> 
    </tr> 
</table> 

</form:form> 
</body> 
</html> 

その後、Person.java

public class Person { 
    private String firstname; 
    private String lastname; 

    //.. getter and setter for all above fields. 
} 

がPersonController.java

public class PersonController { 

    @RequestMapping(value = "/addPerson", method = RequestMethod.POST) 
    public String addContact(@ModelAttribute("person") 
          Person person, BindingResult result) { 

     System.out.println("First Name:" + person.getFirstname() + 
        "Last Name:" + person.getLastname()); 

     return "redirect:person.html"; 
    } 
} 
を作成するためのモデルクラスを作成する必要があります

ViewResolver用spring-servlet.xmlの作成

<?xml version="1.0" encoding="UTF-8"?> 
-<beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"> 

<context:component-scan base-package="net.viralpatel.spring3.controller"/> 


-<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="viewResolver"> 

<property value="org.springframework.web.servlet.view.JstlView" name="viewClass"/> 

<property value="/WEB-INF/jsp/" name="prefix"/> 

<property value=".jsp" name="suffix"/> 

</bean> 

</beans> 
+0

返信いただきありがとうございますが、私はxmlに基づくすべてのものを使用していますが、注釈ではありません。 – JavaTechLead

0

MultiActionControllerを使用できます。

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<html> 
<body> 
<form:form method="post" action="/yourapp/addPerson"> 

    <table> 
    <tr> 
     <td><form:label path="firstname">First Name</form:label></td> 
     <td><form:input path="firstname" /></td> 
    </tr> 
    <tr> 
     <td><form:label path="lastname">Last Name</form:label></td> 
     <td><form:input path="lastname" /></td> 
    </tr> 
    <tr> 
     <td colspan="2"> 
      <input type="submit" value="Add Person"/> 
     </td> 
    </tr> 
</table> 

</form:form> 
</body> 
</html> 

と、これはあなたのモデルクラスです:

public class Person { 
    private String firstname; 
    private String lastname; 

    //.. getter and setter for all above fields. 
} 

あなたのコントローラは次のようになります。

public class PersonController extends MultiActionController{ 

public ModelAndView addPerson(HttpServletRequest request, HttpServletResponse response) throws Exception { 
    //... your code here 
} 

public ModelAndView listPerson(HttpServletRequest request, HttpServletResponse response) throws Exception { 
//..your code here 
} 


} 

とあなたの
のは、これはあなたの.jspファイルであるとしましょう。 XMLファイルは次のようになります。

<?xml version="1.0" encoding="UTF-8"?> 

    <beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

     <!-- for mapping controllers --> 
     <bean name="/addPerson" class="yourpackage.PersonController" /> 
     <bean name="/listPerson" class="yourpackage.PersonController" /> 

     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/view/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
</beans> 

次に例を示します。
http://www.mkyong.com/spring-mvc/spring-mvc-multiactioncontroller-example/

関連する問題