如何在spring mvc,基于xml的配置中重定向到控制器中的特定方法?

时间:2016-05-06 08:37:02

标签: spring-mvc

如何在spring mvc,基于xml的配置中重定向到控制器中的特定方法?

我从JSP提交数据,并希望在控制器中使用特定方法。 我在没有MAVEN的情况下使用基于XML的配置。

由于

2 个答案:

答案 0 :(得分:0)

创建一个JSP文件,该文件将向我们的用户显示Person。 文件:/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创建Model类

    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>

答案 1 :(得分:0)

您可以使用MultiActionController
请说这是您的.jsp文件:

<%@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
}


}

并且.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/