您的位置:首页 > 编程语言 > Java开发

springMVC

2016-03-21 23:14 471 查看
注解方式

package actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloAction {

@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String registPost() {
return "/welcome";
}

//�������Ϊ/springmvc/login/tom.do?id=20
//user����ֵΪtom,id����ֵΪ20
@RequestMapping(value = "/login/{user}", method = RequestMethod.GET)
public ModelAndView myMethod(HttpServletRequest request, HttpServletResponse response,
@PathVariable("user") String user,@RequestParam("id") String id, ModelMap modelMap) throws Exception {
modelMap.put("loginUser", user);
modelMap.put("userId", id);
return new ModelAndView("/hello", modelMap);
}

}


spring-annotation.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 自动扫描Bean -->
<context:component-scan base-package="actions"></context:component-scan>
<!-- 开启Spring MVC注解 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<!-- 视图扩展名解析器 对模型视图名称的解析,即在模型视图名称添加前后缀-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/jsp"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>


struts.xml
<?xml version="1.0" encoding="gbk"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-annotation.xml</param-value>
<!-- spring-annotation.xml是注解方式 -->
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: