[color=red]Web.xml[/color]
[color=red]spring-mvc-serlvet.xml[/color]
[color=red]applicationContext.xml[/color]
PS:对于web无配置
java类
[color=red]Emp.java(实体类)[/color]
[color=red]EmpController.java(控制类)[/color]
[color=red]StartController.java(控制类)[/color]
[color=olive]页面编写[/color]
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 默认路径为/WEB-INFO/[servlet]-servlet.xml ,如这为/WEB-INFO/spring-mvc-servlet.xml -->
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
[color=red]spring-mvc-serlvet.xml[/color]
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="login.do">loginController</prop>
<prop key="login1.do">loginController1</prop>
<prop key="test1.do">empController</prop>
<prop key="test.do">startController</prop>
</props>
</property>
</bean>
<!-- 根据返回值拼装前缀和后缀,返回页面 -->
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 跳转到控制类 -->
<bean id="startController" class="test.StartController"></bean>
<!-- 直接页面跳转,不通过控制类,通过UrlBasedViewResolver的前缀和后缀进行页面拼装并且直接返回页面 -->
<bean id="loginController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="login"></property>
</bean>
<!-- 直接页面跳转另外一种方式,直接根据跳转的url进行跳转,如login1.do,则返回login,根据拼装返回/login1.jsp -->
<bean id="loginController1" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
<!-- 得到javaBean的实体对象控制器 -->
<bean id="empController" class="test.EmpController">
<property name="commandClass" value="test.Emp"></property>
</bean>
[color=red]applicationContext.xml[/color]
PS:对于web无配置
java类
[color=red]Emp.java(实体类)[/color]
public class Emp {
private String name;
private String age;
//…getter和setter
}
[color=red]EmpController.java(控制类)[/color]
public class EmpController extends AbstractCommandController {
protected ModelAndView handle(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors) throws Exception {
Emp emp = (Emp)command;
System.out.println(emp.getName()+" "+emp.getAge()+" phone:"+request.getParameter("phone"));
return null;
}
}
[color=red]StartController.java(控制类)[/color]
public class StartController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("StartController开始启动");
return new ModelAndView("index");
}
}
[color=olive]页面编写[/color]
<a href="simple?name=text&age=28"> simple</a><button onclick="simple()">simple</button><br/>
<a href="simple1?name=text&age=28"> simple1</a><button onclick="simple1()">simple1</button><br/>
<script type="text/javascript">
function simple(){
$.getJSON("test.do",{"name":"nameJsonTest","age":"100"},
function(){});
}
function simple1(){
$.getJSON("test1.do",
{"name":"nameJsonTest","age":"100","phone":"120"},
function(){});
}