我最近在Spring 4/Hibernate Web应用程序中实现了Spring Security,以处理登录/退出和不同的用户角色.
经过大量阅读后,它现在看起来工作得很好,但是我注意到由于错误的Spring Security配置引发的异常没有使用我的自定义处理程序正常处理,但显示为丑陋的Tomcat错误页面(显示HTTP状态500 - UserDetailsService是需要后跟堆栈跟踪).
解决特定错误并不困难(在RememberMe配置中添加userDetailsService(userDetailsService))但事实仍然是抛出的一些异常不由处理MaxUploadSizeExceededException和所有其他运行时异常的ControllerAdvice处理:
@ControllerAdvice
public class ExceptionHandlingControllerAdvice {
public static final String DEFAULT_ERROR_VIEW = "genericerror";
@ExceptionHandler(value = MaxUploadSizeExceededException.class)
public View maxUploadSizeExceededExceptionHandler(
HttpServletRequest req) throws IOException {
String redirectUrl = req.getRequestURL().toString();
RedirectView rv = new RedirectView(redirectUrl);
FlashMap outputFlashMap = RequestContextUtils.getOutputFlashMap(req);
if (outputFlashMap != null) {
outputFlashMap.put(KeyConstants.FLASH_ERROR_KEY, "Bestand is te groot");
}
return rv;
}
@ExceptionHandler(value = RuntimeException.class)
public View defaultErrorHandler(HttpServletRequest req, Exception e) {
RedirectView rv = new RedirectView("/error", true); //context relative
StackTraceElement[] steArray = …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring,Hibernate以及Thymeleaf构建一个应用程序来获取视图.该应用程序以Activity域类为中心(实际上是由俱乐部成员组织的室内或室外活动).该类有几个查找字段(类别区域),它们被建模为由Hibernate映射到数据库查找表的域类.
在"编辑"视图页面中,我显示了一个下拉框,可以选择查找值,还必须使用所选属性显示当前值.这里出错了.尽管布尔表达式看起来是正确的,但从不生成select属性.
添加th:attr以显示比较表达式的值,即$ {choice.id}和*{category.id}这些值是正确的.我添加了readonly属性(不适合选择元素,但只是为了讨论是否可能是th:selected属性的问题),我们在输出中看到此属性是在输出中生成的!
<label>Categorie</label>
<select th:field="*{category}">
<option th:each="choice : ${allCategories}"
th:value="${choice.id}"
th:attr="choiceid=${choice.id}, categoryid=*{category.id}, showselected=(${choice.id} == *{category.id})"
th:selected="(${choice.id} == *{category.id})"
th:readonly="(${choice.id} == *{category.id})"
th:text="${choice.description}"></option>
</select>
Run Code Online (Sandbox Code Playgroud)
HTML输出:
<label>Categorie</label>
<select id="category" name="category">
<option choiceid="1" categoryid="2" showselected="false" value="1">Actief en sportief</option>
<option choiceid="2" categoryid="2" showselected="true" readonly="readonly" value="2">Uitgaan en nachtleven</option>
<option choiceid="3" categoryid="2" showselected="false" value="3">Kunst en cultuur</option>
<option choiceid="4" categoryid="2" showselected="false" value="4">Eten en drinken</option>
<option choiceid="5" categoryid="2" showselected="false" value="5">Ontspanning en gezelligheid</option>
</select>
Run Code Online (Sandbox Code Playgroud)
所以我的任务归结为:为什么被 选中="被选中"
没有为id = 2的选项元素生成?
当然,它可以通过使用th:attr表达式生成来实现,但根据文档,它应该以这种方式工作.
此外,我试图绕过使用特殊的th:selected属性并使用th:attr为所选选项生成所选属性(值'none'是临时用于清晰'):
th:attr ="selected =($ {choice.id} ==*{category.id})?'selected':'none'"
同样,没有显示任何内容,也没有呈现选定的属性.我尝试重复的是使用自定义属性名称(所选单词的荷兰语翻译,当然不为Thymeleaf所知): …
我正在使用Spring MVC(和Hibernate)处理应用程序,并对配置问题感到有点困惑.设置和配置Spring应用程序的方式有很多简单,这有点简单,有时甚至会在整个教程中混合使用......
我使用Spring 4和纯Java配置,因此没有XML配置文件.应用程序的入口点是AbstractAnnotationConfigDispatcherServletInitializer的子类:
public class MvcWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {
HibernateConfig.class,
ServiceConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {MvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
Run Code Online (Sandbox Code Playgroud)
我们看到有2个根配置类,分别用于加载Hibernate和Service配置类,还有一个用于加载Service配置类(实际上除了扫描~.service包下的conponents之外).
HibernateConfig是唯一需要application.properties文件属性的文件,因此该文件作为PropertySource读取并在类中使用,如下所示:
package nl.drsklaus.activiteitensite.configuration;
//imports
@Configuration
@EnableTransactionManagement
@ComponentScan({"nl.mydomain.activiteitensite.dao"})
@PropertySource(value= {"classpath:application.properties"})
public class HibernateConfig {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
//code
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new …Run Code Online (Sandbox Code Playgroud) 我最近跳上了CodeIgniter列车,因为我最终想要为我不断发展的PHP/MySQL项目采用MVC架构.现在我遇到了一个probolemin,就我所能看到的那个早先没有描述过的PAgination configuratino.
可能问题不在于PAgination只是相关而是更一般.在我的第一次尝试中,我只是在控制器方法中填充了配置并将其传递给initialize()方法:
$config['base_url'] = $this->config->base_url() . "/categorie/{$catName}/";
$config['total_rows'] = $this->event_model->get_nrofevents_for_categorie($categorieObject->id, TRUE);
$config['per_page'] = 12;
$config['full_tag_open'] = '<div class="paging">';
$config['full_Tag_close'] = '</div>';
$config['prev_link'] = 'Vorige';
$config['next_link'] = 'Volgende';
$this->pagination->initialize($config);
Run Code Online (Sandbox Code Playgroud)
这很好用.但我有一些其他页面使用相同的分页,其大多数参数相同,只有base_url和total_rows配置属性对于每个页面是不同的.我已将公共配置放在config/pagination.php配置文件中,但没有看到在代码中添加依赖于页面的属性的选项.更一般的是,是否可以将通用配置放在配置文件中并在控制器方法中添加一些自定义属性?对我来说,这似乎是合乎逻辑的,但无法弄清楚如何......我尝试过类似的东西:
$this->load->config('pagination');
$this->config->set_item('next_link', 'blablabla');
Run Code Online (Sandbox Code Playgroud)
但似乎在读取配置文件后立即初始化分页,此代码无效.请问有什么建议?