初学springmvc,做一简单的配置
(1)web.xml
<span style="color:#3333ff;"> <span style="white-space:pre"> </span><servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name></span>
<span style="color:#3333ff;"><span style="white-space:pre"> </span>//配置当前springmvc的文件路径
<param-value>classpath*:/config/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping></span>
(2)spring-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<span style="white-space:pre"> </span>//配置JavaBean
<span style="color:#00cccc;"> </span><span style="color:#3333ff;"> <bean name="/test/hello" class="com.controller.HelloWorldContorller"></bean>
</span>
<span style="color:#3333ff;">//视图解析器
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean></span>
</beans>
(3)java代码
public class HelloWorldContorller implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
String str = "slfskf";
return new ModelAndView("/welcome","str",str);
}
访问失败(404)
警告:No mapping found for HTTP request with URI [/springmvc1/test] in DispatcherServlet with name 'spring'
正确 <bean name="/test/hello" class="com.controller.HelloWorldContorller"></bean>
404 <bean name="test/hello" class="com.controller.HelloWorldContorller"></bean>
springmvc多action配置
(1)spring-servlet.xml
<span style="white-space:pre"> </span><span style="font-size:18px;"><bean name="/test1/multi" class="com.controller.MultiController">//java类名
<property name="methodNameResolver">
<span style="background-color: rgb(255, 204, 51); "><ref bean="paramMethodReolver"/>//指向参数名解析器id</span>
</property>
</bean></span>
<span style="font-size:18px;"><!-- 参数名解析器-->
<bean id="paramMethodReolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<span style="background-color: rgb(255, 153, 0);"><property name="paramName" value="action"></property></span> //所要使用的参数
</bean></span>
(2)java代码
<span style="font-size:18px;">public class MultiController extends MultiActionController {
public ModelAndView add(HttpServletRequest request,HttpServletResponse response){
//没有两个参数,请求失败404
return new ModelAndView("/multi","method","add");
}
public ModelAndView update(HttpServletRequest request,HttpServletResponse response){
return new ModelAndView("/multi","method","update");
}</span>
}
(3)访问
http://localhost:8080/springmvc/test1/multi?action=add\update