SpringMVC模式各种配置以及各种控制器

[color=red]Web.xml[/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(){});
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值