SpringMVC程序简单实例
程序员文章站
2023-11-18 08:38:28
stringmvc程序简单实例
第一步:导入jar包
第二步,在web-inf文件夹下创建spring-servlet.xml文件。
...
stringmvc程序简单实例
第一步:导入jar包
第二步,在web-inf文件夹下创建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" xmlns:aop="http://www.springframework.org/schema/aop" 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"> <!-- 注册aop功能 --> <aop:aspectj-autoproxy/> <!-- 自动扫面com.springmvc目录及其子目录下面所有类文件,自动注入所有带注解的类 --> <context:component-scan base-package="com.yxl.*" /> <!-- 处理请求response返回值,如下配置能正确返回字符串型返回值,如返回值为对象,则自动转为json --> <bean id="handleadapter" class="org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter"> <property name="messageconverters"> <list> <ref bean="mappingjacksonhttpmessageconverter" /><!-- json转换器 --> <ref bean="mappingstringhttpmessageconverter" /> </list> </property> </bean> <bean id="mappingjacksonhttpmessageconverter" class="org.springframework.http.converter.json.mappingjacksonhttpmessageconverter" /> <bean id="mappingstringhttpmessageconverter" class="org.springframework.http.converter.stringhttpmessageconverter" /> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/jsp/" /> <property name="suffix" value=".jsp"></property> <property name="order" value="1"></property> </bean> <!-- spring文件上传编码 --> <bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver" p:defaultencoding="utf-8" /> </beans>
第三步:在web.xml文件配置springmvc。
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="webapp_id" version="2.5"> <display-name>springmvc1</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 设置编码 --> <filter> <filter-name>characterencodingfilter</filter-name> <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceencoding</param-name> <param-value>true</param-value> </init-param> </filter> <!-- 配置dispatchcerservlet --> <servlet> <servlet-name>spring mvc dispatcher servlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <!-- 配置spring mvc下的配置文件的位置和名称 --> <param-name>contextconfiglocation</param-name> <param-value> /web-inf/spring-servlet.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置dispatchcerservlet 拦击路径'/'所有访问都拦截--> <servlet-mapping> <servlet-name>spring mvc dispatcher servlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
第四步:创建一个控制器。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: visio怎么绘制动画片中出现的魔法阵?